package com.kevin.Code.Hash;

/**
 * @author Vinlee Xiao
 * @Classname ValidSudoku
 * @Description Leetcode 36. 有效的数独 中等难度 思路同Leetcode 37
 * @Date 2021/10/25 11:30
 * @Version 1.0
 */
public class ValidSudoku {


    /**
     * @param board
     * @return
     */
    public boolean isValidSudoku(char[][] board) {

//        int row = board.length;
        //用于存储行号 值
        boolean[][] rows = new boolean[9][9];
        // 列号 值
        boolean[][] cols = new boolean[9][9];
        //坐标  值
        boolean[][][] area = new boolean[3][3][9];


        for (int i = 0; i < 9; i++) {

            for (int j = 0; j < 9; j++) {


                if (board[i][j] != '.') {

                    //判断行数组中是否存在
                    if (rows[i][board[i][j] - '0' - 1]) {
                        return false;
                    } else {
                        rows[i][board[i][j] - '0' - 1] = true;
                    }

                    //判断列 是否满足
                    if (cols[j][board[i][j] - '0' - 1]) {
                        return false;
                    } else {
                        cols[j][board[i][j] - '0' - 1] = true;
                    }


                    //判断小区域是否合格
                    int r = i / 3;
                    int c = j / 3;
                    if (area[r][c][board[i][j] - '0' - 1]) {
                        return false;
                    } else {
                        area[r][c][board[i][j] - '0' - 1] = true;
                    }

                }
            }

        }

        return true;
    }
}
